package com.csdj.service;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mysql.cj.log.Log;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisPubSub;
import redis.clients.jedis.params.SetParams;

import javax.annotation.PostConstruct;
import java.util.Map;

@Service
public class RedisService {
    RedisService(){
        System.out.println("创建RedisService对象");
    }

    private final Logger log = Logger.getLogger("RedisService");

    private static JedisPool pool = null;
    @Value("${redis.host:localhost}")
    private String host;
    @Value("${redis.port:6379}")
    private int port;
    @Value("${redis.password:123456}")
    private String password;

    //String host,int port,String password
    //在构造方法之后注入值
    @PostConstruct
    public void init(){
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMinIdle(2);
        config.setMaxTotal(20);
        pool = new JedisPool(config,host, port,3000,password);

    }

    public void subsribe(String channel, JedisPubSub task){
        Jedis resource = pool.getResource();

        //启动新线程
        new Thread(()->{
            if(resource != null ){
                resource.subscribe(task,channel);
            }
        }).start();

    }

    public boolean publish(String channel,String msg){
        Jedis resource = pool.getResource();
        try{

            if(resource != null ){
                resource.publish(channel, msg);
                return true;
            }
            return false;
        }finally {
            if(resource != null){
                System.out.println("finally 回收resource");
                resource.close();
            }

        }
    }


    public  Long del(String key){
        Jedis resource = null;
        try {
            resource = pool.getResource();
            if(resource!=null){
                return resource.del(key);
            }
            return null;
        }finally {
            if(resource != null){
                //close回收
                resource.close();
            }
        }
    }

    public  Boolean exists(String key) {
        Jedis resource = null;
        try {
            resource = pool.getResource();
            if (resource != null) {
                return resource.exists(key);
            }
        } finally {
            if (resource != null) {
                //close回收
                resource.close();
            }
        }
        return null;
    }

    public  Long explpe(String key, int seconds){
        Jedis resource = null;
        try {
            resource = pool.getResource();
            if (resource != null) {
                return resource.expire(key,seconds);
            }
        } finally {
            if (resource != null) {
                //close回收
                resource.close();
            }
        }
        return null;
    }

    public  Long ttl(String key){
        Jedis resource = null;
        try {
            resource = pool.getResource();
            if(resource!=null){
                return resource.ttl(key);
            }
            return null;
        }finally {
            if(resource != null){
                //close回收
                resource.close();
            }
        }
    }

    public  Long decr(String key){
        Jedis resource = null;
        try {
            resource = pool.getResource();
            if(resource!=null){
                return resource.decr(key);
            }
            return null;
        }finally {
            if(resource != null){
                //close回收
                resource.close();
            }
        }
    }

    public  Long incr(String key){
        Jedis resource = null;
        try {
            resource = pool.getResource();
            if(resource!=null){
                return resource.incr(key);
            }
            return null;
        }finally {
            if(resource != null){
                //close回收
                resource.close();
            }
        }
    }


    public  String setString(String key,String value ){
        Jedis resource = null;
        try {
            resource = pool.getResource();
            if(resource!=null){
                return resource.set(key,value);
            }
            return null;
        }finally {
            if(resource != null){
                //close回收
                resource.close();
            }
        }
    }

    public  String hget(String key,String field){
        Jedis resource = null;
        try {
            resource = pool.getResource();
            if(resource!=null){
                return resource.hget(key,field);
            }
            return null;
        }finally {
            if(resource != null){
                //close回收
                resource.close();
            }
        }
    }

    public  Map<String, String> hgetAll(String key){
        Jedis resource = null;
        try {
            resource = pool.getResource();
            if(resource!=null){
                return resource.hgetAll(key);
            }
            return null;
        }finally {
            if(resource != null){
                //close回收
                resource.close();
            }
        }
    }

    public static Long hset(String key, String field, String value){
        Jedis resource = null;
        try {
            resource = pool.getResource();
            if(resource!=null){
                return resource.hset(key,field,value);
            }
            return null;
        }finally {
            if(resource != null){
                //close回收
                resource.close();
            }
        }
    }


    public static String getString(String key){
        Jedis resource = pool.getResource();
        try{

            if(resource != null ){
                return resource.get(key);
            }
            return null;
        }finally {
            if(resource != null){
                System.out.println("finally 回收resource");
                resource.close();
            }

        }
    }

    /**
     *
     * @param key
     * @param object
     * @param sec   缓存时间
     * @return
     */
    public boolean setObj(String key ,Object object,int sec)  {
        Jedis resource = pool.getResource();
        try{

            if(resource != null ){
                //jackson JSON转换器
                ObjectMapper om = new ObjectMapper();
                SetParams setParams = new SetParams();
                setParams.ex(sec);
                String res = null;
                try {

                    res = resource.set(key, om.writeValueAsString(object),setParams);
                } catch (JsonProcessingException e) {
                    log.error(e.getClass(),e);
                    return false;
                }
                if("OK".equalsIgnoreCase(res)){
                    return true;
                }
            }
            return false;
        }finally {
            if(resource != null){
                System.out.println("finally 回收resource");
                resource.close();
            }

        }
    }

    public <T>T getObj(String key,Class<T>clazz ){
        Jedis resource = pool.getResource();
        try{

            if(resource != null ){
                String res= resource.get(key);
                //判断是否不为空
                if(StringUtils.hasText(res)){
                    ObjectMapper om = new ObjectMapper();
                    return om.readValue(res,clazz);
                }
            }
            return null;
        } catch (JsonMappingException e) {
            log.error(e.getClass(),e);
            return null;
        } catch (JsonProcessingException e) {
            log.error(e.getClass(),e);
            return null;
        } finally {
            if(resource != null){
                System.out.println("finally 回收resource");
                resource.close();
            }

        }

    }



}
